package com.queue.common.protocol;

import com.alibaba.fastjson.JSON;
import com.queue.common.message.Message;

import java.nio.charset.StandardCharsets;

public class MessageUtils {

    /**
     * 将消息内容转换成字节数组
     *
     * @param message
     * @return
     */
    public static byte[] change(Message message){
        return JSON.toJSONString(message).getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 将字节数组转换成message对象
     *
     * @param bytes
     * @return
     */
    public static Message reverse(byte[] bytes){
        return JSON.parseObject(new String(bytes,StandardCharsets.UTF_8),Message.class);
    }

    public static Protocol messageToProtocol(Message message){
        byte[] content = JSON.toJSONString(message).getBytes(StandardCharsets.UTF_8);
        Protocol protocol = new Protocol(content.length, content);
        return protocol;
    }
}
